/*
 * @lc app=leetcode.cn id=47 lang=javascript
 *
 * [47] 全排列 II
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permuteUnique = function(nums) {
    if (!nums || !nums.length) return []
    let result = []
    nums.sort((a, b) => a - b)

    const len = nums.length
    const has = {}
    let tmpT = (tmp) => {
        if (tmp.length === len) {
            result.push(tmp)
            return
        }
        for(let i = 0; i< len;i++) {
            if (has[i] || (i > 0 && !has[i - 1] && nums[i] === nums[i - 1])) continue
            tmp.push(nums[i])
            has[i] = true
            tmpT(tmp.slice())
            has[i] = false
            tmp.pop()
        }
    }
    tmpT([])
    return result
};
// @lc code=end

